﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SBB.WinForms.SFTP.ClientTest
{
    public partial class Form1 : Form
    {
        private Stream _file;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                _file = fileDialog.OpenFile();

                fileStatus.Text = "File select. Press Send to transfer file to server";
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            fileStatus.Text = "Transferring file...";

            using (var memStream = new MemoryStream())
            using (var sbbClient = new SBBClient.ImportToDBClient()) {

                sbbClient.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;
                sbbClient.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential("renier.stander", "brUBus3-", "TIGERS.local");

                _file.CopyTo(memStream);

                sbbClient.Submit(new SBBClient.TransactionItemsComposite {
                    ContentIdentifier = "RenierTestUpload",
                    SenderIdent = "Tigers\renier.stander",
                    CallingSystemId = "RENIERSTANDER",
                    myPayload = new SBBClient.TransactionItemsComposite.DataPayload[] {
                        new SBBClient.TransactionItemsComposite.DataPayload {
                            fileName = Path.GetFileName(fileDialog.FileName),
                            fileArray = memStream.ToArray()                            
                        }
                    }
                });

                fileStatus.Text = "File transfer successful!";
            }
        }
    }
}
